package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
)

func QiNiuYun(file multipart.File, m *multipart.FileHeader, filename string) (string, error) {

	accessKey := "G9quwBhtC7kg0IFXEaS3L2e6GC5o-wzVvC2uktrE"
	secretKey := "nRjd7Z8YhMSimm3606T0T-jzeth6EfoheAPqYU3J"
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := "libangbang"
	key := filename
	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadReader(context.Background(), file, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		FileName:   key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	if err != nil {
		return "", err
	}

	return "http://sm3me6jec.hn-bkt.clouddn.com/" + filename, nil
}
